from datetime import datetime, timedelta
from nanorm import *


def set_time(time_str):
    """
    字符串转时间类
    :param "2023-05-01 9:06:00"
    :return: 2023-05-01 9:06:00
    """
    result = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
    return result


class Task:
    def __init__(self, user_id, task_id, description, category, during, priority, start=False):
        self.user_id = user_id
        self.task_id = task_id
        self.description = description
        self.category = category
        self.during = during
        self.priority = priority
        self.start = start

    def get_id(self):
        return self.user_id

    def get_priority(self):
        return self.priority

    def set_priority(self, priority):
        self.priority = priority

    def get_description(self):
        return self.description

    def set_description(self, description):
        self.description = description

    def get_category(self):
        return self.category

    def set_category(self, category):
        self.category = category

    def get_during(self):
        return self.during

    def set_during(self, during):
        self.during = during

    def __str__(self):
        return f"user_id:{self.user_id} task_id:{self.task_id} " \
               f"description:{self.description} category:{self.category} " \
               f"during:{self.during} priority:{self.priority} " \
               f"start:{self.start}"


# 继承Task,添加多三个属性属性,start_time_str ,end_time_str start=1，finish=True 完成的任务 finish=False 中途放弃的任务
# finish的计算可以通过 finish_time-start_time和freq 来计算
class StartedTask(Task):
    def __init__(self, start_time_str, end_time_str, user_id, task_id, description, category, during, priority):
        super().__init__(user_id, task_id, description, category, during, priority, True)
        self.start_time_str = start_time_str
        self.end_time_str = end_time_str
        self.finish = self.check_finished()
        self.real_time = self.cal_time() / 60

    def cal_time(self):
        start_time = set_time(self.start_time_str)
        end_time = set_time(self.end_time_str)
        return int((end_time - start_time).total_seconds())

    def check_finished(self):
        result = self.cal_time() == self.during * 60
        return result

    def get_start_time_str(self):
        return self.start_time_str

    def get_end_time_str(self):
        return self.end_time_str

    def get_finish(self):
        return self.finish

    def __str__(self):
        return f"start_time_str:{self.start_time_str} " \
               f"end_time_str:{self.end_time_str} " \
               f"finish:{self.finish} " \
               f"user_id:{self.user_id} task_id:{self.task_id} " \
               f"description:{self.description} category:{self.category} " \
               f"during:{self.during} priority:{self.priority} " \
               f"start:{self.start}"


# class User_Info(Model, UserMixin):
#     """
#     用户信息表
#     """
#     user_name = CharField(128)
#     password = CharField(128)
#     active = IntegerField()
#     email = CharField(128)
#
#     def get_id(self):
#         return self.user_name
#
#     def get_by_id(self, user_name):
#         try:
#             userinfo = User_Info.get(user_name=user_name)
#             return userinfo
#         except:
#             pass
#         return None
#
#     def __str__(self):
#         return str(self.to_dict())
#
#     def to_dict(self):
#         dict_my = {}
#         for key, value in zip(self.field_names, self.field_values):
#             dict_my[key.replace("`", "")] = value.replace("'", "")
#         return dict_my
#
#
#     @staticmethod
#     def is_user_exist():
#         """
#         判断user_info表是否为空
#         :return:
#         """
#         try:
#             sql = "select * from %s limit 1" \
#                   % (__class__.__name__.lower())  # 使用 limit 1 来限制结果集的大小，只获取第一行数据。
#             cu = get_cursor()  # 获取光标
#             execute_sql(cu, sql)  # execute_sql函数的目的是执行SQL查询语句并将结果存储在游标（cursor）中。
#             rows = cu.fetchall()
#             if len(rows) >= 1:
#                 return True
#         except:
#             lock.release()
#             pass
#         return False


# class Everyday_Cache(Model):
#     """
#     每日缓存表
#     """
#     user_id = "1"
#     date_str = CharField(10)
#     week_start_str = CharField(10)
#     week_end_str = CharField(10)
#     year_str = CharField(4)
#     month_str = CharField(7)
#     during = IntegerField()
#     nums = IntegerField()
#
#     def __str__(self):
#         return str(self.to_dict())
#
#     def to_dict(self):
#         dict_my = {}
#         for key, value in zip(self.field_names, self.field_values):
#             dict_my[key.replace("`", "")] = value.replace("'", "")
#         return dict_my
#
#     @staticmethod
#     def get_table_name():
#         return __class__.__name__.lower()
#
#
# # if __name__ == "__main__":
# # user_id = 1
#     # task_id = 2
#     # description = "sleep"
#     # category = "rest"
#     # during = 1
#     # priority = 1
#     # start_time_str = "2023-05-01 09:06:00"
#     # end_time_str = "2023-05-01 09:07:00"
#     # task = Task(user_id, task_id, description, category, during, priority)
#     # task.set_priority(2)
#     # finished_task = StartedTask(start_time_str, end_time_str, user_id, task_id, description, category, during, priority)
#     # print(finished_task)
